package cn.tedu._03vehicle.controller;


import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.UserMapper;
import cn.tedu._03vehicle.pojo.dto.UserLoginParam;
import cn.tedu._03vehicle.pojo.dto.UserRegParam;
import cn.tedu._03vehicle.pojo.entity.User;
import cn.tedu._03vehicle.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
@Tag(name="01-用户注册模块")
@Slf4j
@RestController
@RequestMapping("/v1/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    /**
     * 用户注册
     */
    @PostMapping("reg")
    @Operation(summary = "1.用户注册")
    @ApiOperationSupport(order = 10)
    public JsonResult reg(@Validated @RequestBody UserRegParam userRegparam) {
        log.debug("userRegparam = "+ userRegparam);
        /*
        1.接收客户端传递的参数【已完成】
        2.效验数据的合法性【暂时不效验，后续使用验证框架validation实现效验】
        3.效验用户名是否被占用【SELECT接口】
          SELECT COUNT(*) FROM user WHERE username = ?
          3.1.如果用户名被占用，返回【用户名被占用】
          3.2.如果用户名可用，执行插入【INSERT接口】,返回注册成功
         */
        String username = userRegparam.getUsername();
        int rows = userMapper.selectByUsername(username);
        if (rows > 0 ) { //被占用
            return new JsonResult(StatusCode.USERNAME_EXISTS);
        }//未被占用
        User user = new User();
        BeanUtils.copyProperties(userRegparam, user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        log.debug("user = " + user);
        userMapper.insertUser(user);
        return JsonResult.ok();
    }

    /**
     * 用户登录
     * */
    @PostMapping("login")
    @Operation(summary = "2.登录功能")
    @ApiOperationSupport(order = 20)
    public JsonResult login(@RequestBody UserLoginParam userLoginParam, HttpSession session){
        log.debug("userLoginParam = " + userLoginParam);
        UserVO userVO = userMapper.selectByUsernameAndPassword(userLoginParam);
        if (userVO == null) {
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
        //将用户信息保存到sdssion中
        session.setAttribute("user", userVO);
        //登录成功
        return JsonResult.ok(userVO);
    }

    /**
     * 获取当前用户功能
     */
    @GetMapping("currentUser")
    @Operation(summary = "3.获取当前用户功能")
    @ApiOperationSupport(order = 30)
    public JsonResult currentUser(HttpSession session){
        //服务器内存中获取回话标识
        UserVO userVO = (UserVO) session.getAttribute("user");
        //后端情况1：{"state":2000,"message":"操作成功”,"data":userVO}
        //后端情况2：{"state":2000,"message":"操作成功”,"data":null}
        //前端工程师判断【JavaScript语言】: if(data != null){...}else{...}
        return JsonResult.ok(userVO);
    }

    /**  退出登录功能
     * 测试思路：
     * 1.登陆成功
     * 2.获取当前用户信息【可以获取到】
     * 3.退出登录
     * 4.获取当前用户信息【获取不到】
     */

    @GetMapping("logout")
    @Operation(summary = "4.退出登录功能")
    @ApiOperationSupport(order = 40)
    public void logout(HttpSession session){
        //在服务器内存中删除回话标识
        session.removeAttribute("user");
    }
}
